<?php
namespace App\Http\Controllers\AppApi\Home;

use App\Models\Admin\OrderBaleModel;
use App\Services\Admin\OrderBaleService;
use App\Services\Main\BaleCostService;
use App\Services\Main\ComContractService;
use Illuminate\Http\Request;

class CostController extends BaseController
{
    /**
     * 加工费
     * 制作者：九哥
     */

    protected $service;
    protected $contractService;
    protected $baleService;

    public function __construct()
    {
        parent::__construct();
        $this->service = new BaleCostService();
        $this->contractService = new ComContractService();
        $this->baleService = new OrderBaleService();
    }

    /**
     * 原料面料，分类设置
     */
    public function setTypeAdd(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $orderId = $request->orderId;
        $typeName = $request->typeName;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        if (!$typeName) { return $this->toJson(-2); }
        //更新类型
        $lastId = $this->service->setTypeAdd($orderId,$typeName);
        if (!$lastId) { return $this->toJson(-26); }
        $dataArr = array(
            'typeId' => $lastId,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 原料面料，分类设置
     */
    public function setTypeModify(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $orderId = $request->orderId;
        $typeId = $request->typeId;
        $typeName = $request->typeName;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        if (!$typeName || !$typeId) { return $this->toJson(-2); }
        //更新类型
        $this->service->setTypeModify($typeId,$typeName);
        $dataArr = array(
            'typeId' => $typeId,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 原料面料清单获取
     * /api/home/release/fabric/typelist
     */
    public function getCostTypes(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $orderId = $request->orderId;
        if (!$token || !$orderId) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }

        $dataArr = $this->service->getTypes($orderId);

        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 原料面料，新增
     */
    public function setAdd(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $typeId = $request->typeId;
        $name = $request->name;
        $num = $request->num;
        if (!$token) { return $this->toJson(-2); }
        if (!$name || !$num) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $dataArr = array(
            'type' => $typeId,
            'name' => $name,
            'cost' => $num,
        );
        $lastId = $this->service->add($dataArr);
        if (!$lastId) { return $this->toJson(-26); }
        $dataArr = array(
            'costId' => $lastId,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 更新原料name
     */
    public function setModifyName(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $typeId = $request->typeId;
        $costId = $request->costId;
        $name = $request->subName;
        if (!$token) { return $this->toJson(-2); }
        if (!$name || !$costId) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $lastId = $this->service->setModifyName($typeId,$costId,$name);
        if (!$lastId) { return $this->toJson(-26); }
        return $this->toJson(0);
    }

    /**
     * 更新原料cost
     */
    public function setModifyCost(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $costId = $request->costId;
        $cost = $request->num;
        if (!$token) { return $this->toJson(-2); }
        if (!$cost || !$costId) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $lastId = $this->service->setModifyCost($costId,$cost);
        if (!$lastId) { return $this->toJson(-26); }
        return $this->toJson(0);
    }

    /**
     * 设置面料状态
     */
    public function setFabricStatus(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $orderId = $request->orderId;
        if (!$token) { return $this->toJson(-2); }
        if (!$orderId) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $baleArr = array(
            'is_fabric' => 3,
        );
        $this->baleService->modify($baleArr,$orderId);
        return $this->toJson(0);
    }

    /**
     * 设置订单合同
     * /api/home/release/contract/add
     */
    public function setContractAdd(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $orderId = $request->orderId;
        $img = $request->img;
        if (!$token) { return $this->toJson(-2); }
        if (!$orderId || !$img) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $lastId = $this->contractService->setAdd($orderId,$uid,$img);
        if (!$lastId) { return $this->toJson(-26); }
        //更新订单合同状态
        $baleArr = array(
            'is_contract' => 3,
            'status'=>OrderBaleModel::status_kaishi
        );
        $this->baleService->modify($baleArr,$orderId);
        return $this->toJson(0);
    }
}